package com.csun.nusing.familypresenter;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.csun.nusing.R;
import com.csun.nusing.bean.UrlBean;
import com.csun.nusing.bean.jsonbean.AlertShowAllJsonBean.AlertShowAllDataBean.AlertShowAllListBean;
import com.csun.nusing.bean.jsonbean.FamilyAlarmListJsonBean;
import com.csun.nusing.bean.jsonbean.FamilyAlarmListJsonBean.FamilyAlarmListDataBean;
import com.csun.nusing.presenter.BasePresenter;
import com.csun.nusing.presenter.inter.INursingOldrest;
import com.csun.nusing.ui.adapter.MyBaseAdapter;
import com.csun.nusing.ui.adapter.ViewHolder;
import com.csun.nusing.util.SPUtils;
import com.csun.nusing.util.TimeUtils;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.home.administrator.okhttplibrary.IOkTaskCallback;
import com.home.administrator.okhttplibrary.OkHttpTask;
import com.home.administrator.okhttplibrary.OkHttpTask.OkHttpTaskBuidler.OkType;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 单个老人的报警列表界面的p层 ClassName: OlderListPresenter
 * 
 * @Description: TODO
 * @author fzq
 * @date 2017-10-23
 */
public class FamilyOlderAlarmListPresenter extends BasePresenter<INursingOldrest> {

	private Context mcontext;
	private String authorization;
	protected FamilyAlarmListJsonBean alertShowAllJsonBean;
	private PullToRefreshListView mlistview;
	private String oldName;
	protected String oldPicturePath;

	public FamilyOlderAlarmListPresenter(INursingOldrest view) {
		super(view);
	}

	public void getAlarmListDate(Context context, String olderId,
			String oldName, PullToRefreshListView listview, String isReadFalg,
			String aletType, String starttime, String endtime) {
		this.mcontext = context;
		this.mlistview = listview;
		this.oldName = oldName;

		/**
		 * 请求数据
		 */

		authorization = (String) SPUtils.get(context, "authorization", "");
		Map<String, String> param = new HashMap<String, String>();
		param.put("pageNo", "1");
		param.put("pageSize", "1000");
		param.put("oldmanId", "" + olderId);
		param.put("isRead", isReadFalg);
		param.put("aletType", aletType);
		param.put("startTime", starttime);
		param.put("endTime", endtime);
		OkHttpTask.newInstance(context).post(param, OkType.form)
				.addHeader("authorization", authorization)
				.enqueue(new IOkTaskCallback() {
					@Override
					public void onSuccess(String result) {
						if (result != null) {
							Gson gson = new Gson();
							alertShowAllJsonBean = gson.fromJson(result,
									FamilyAlarmListJsonBean.class);
							if (alertShowAllJsonBean != null) {
								if (alertShowAllJsonBean.getData() != null) {							
										controlListView(alertShowAllJsonBean
												.getData());
									
								}
							}
						}
					}
				}).start(UrlBean.FAMILYALARMLISTSHOWURL);


	}

	/**
	 * 加载单个老人的数据
	 * 
	 * @Description: TODO
	 * @param @param alertShowAllJsonBean2
	 * @return void
	 * @throws
	 * @author fzq
	 * @date 2017-11-2
	 */
	protected void controlListView(List<List<FamilyAlarmListDataBean>> list) {
		if (list.size()!=0) {
			
		}
		// 处理数据
		MyBaseAdapter<FamilyAlarmListDataBean> myBaseAdapter = new MyBaseAdapter<FamilyAlarmListDataBean>(
				mcontext, list.get(0), R.layout.item_family_alarm_list) {
			@Override
			public void convert(ViewHolder helper,int position, FamilyAlarmListDataBean item) {
				// TODO Auto-generated method stub
				TextView statetv = helper
						.getView(R.id.item_familyalarm_list_state_tv);
				TextView timetv = helper
						.getView(R.id.item_familyalarm_list_time_tv);
				TextView placetv = helper
						.getView(R.id.item_familyalarm_list_place_tv);
				TextView phonetv = helper
						.getView(R.id.item_familyalarm_list_phone_tv);
				Button textView = helper
						.getView(R.id.item_familyalarm_list_button_btn);
				ImageView headimageView = helper
						.getView(R.id.item_familyalarm_list_head_iv);
				ImageView stateview = helper
						.getView(R.id.item_familyalarm_list_state_view);
				if (item.getHandleStatus()==1) {
					stateview.setImageResource(R.drawable.baojing_list_zhuangtai02);
					textView.setVisibility(View.GONE);
				}else{
					stateview.setImageResource(R.drawable.baojing_list_zhuangtai01);
					textView.setVisibility(View.GONE);
				}
				statetv.setText("" + oldName);
				timetv.setText(""
						+ TimeUtils.millis2String(item.getCreateTime()));
				placetv.setText("" + item.getAlertLocation());
				phonetv.setText("" + item.getAlertInfo());
				Glide.with(mcontext).load("" + item.getResourcePath()).into(headimageView);
			}
		};
		mlistview.setAdapter(myBaseAdapter);
	}

	/**
	 * 回调点击事件,点击的位置,将点击的item的内容回调出来 ClassName: MyListener
	 * 
	 * @Description: TODO
	 * @author fzq
	 * @date 2017-10-25
	 */
	public class MyNursingAlarmListener implements OnClickListener {
		private AlertShowAllListBean nursingOldrestListBean;

		public MyNursingAlarmListener(AlertShowAllListBean item) {
			this.nursingOldrestListBean = item;
		}

		@Override
		public void onClick(View v) {
			getViewRef().clickBtn(nursingOldrestListBean);
		}
	}

}
